package com.song.bingclient;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.ShutdownChannelGroupException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.alibaba.fastjson.JSON;
import com.song.corebean.MessageType;
import com.song.corebean.PackageHead;
import com.song.corebean.PackageMessage;
import com.song.corebean.ProsessBean;
import com.song.corebean.utils.CoreUtils;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import io.netty.util.concurrent.GenericFutureListener;

public class GridClient {

	private EventLoopGroup group ;
	
	private ChannelFuture future;
	
	private GridClientHandler handler;
	
	
	public void  connect(String addr , int port)
	{
		EventLoopGroup group = new NioEventLoopGroup();
		this.group = group;
		this.handler = new GridClientHandler();
		Bootstrap bootstrap = new Bootstrap();
		bootstrap.group(group).channel(NioSocketChannel.class)
        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100)
		.handler(new ChannelInitializer<SocketChannel>() {
					@Override
					protected void initChannel(SocketChannel ch)
							throws Exception {
						ChannelPipeline p = ch.pipeline();
						p.addLast(new ObjectEncoder(), new ObjectDecoder(
								ClassResolvers.cacheDisabled(null)));
						p.addLast(handler);
						p.addLast(new ReadTimeoutHandler(5000));
					}

				});
		try {
			this.future = bootstrap.connect(addr, port).sync();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	public void Release()
	{
		this.group.shutdownGracefully();
	}
	public Object  sendAndRecv(MessageType messageType , String key ,Object val)
	{
		try {
			PackageMessage msg = CoreUtils.dataToPackageMessage(messageType, key, val);
			this.future.channel().writeAndFlush(msg).sync();
			
			while(true)
			{
				if(!this.handler.isEmpQueue())
					break;
			}
			return this.handler.readMsg().body.bundle.get("data");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) throws FileNotFoundException, IOException {
		GridClient client = new GridClient();
		List<String> data = IOUtils.readLines(new FileInputStream(new File("C:/Documents and Settings/Administrator/桌面/a.txt")));
		client.connect("192.168.2.114", 58001);
		Object result = client.sendAndRecv(MessageType.DEAL_ALL_DATA,"data",data);
		client.Release();
		
	}
}
